<template>
  <div class="users-wrapper">
    <!-- 搜索 -->
    <div class="search-wrapper">
      <el-input
        v-model="queryModel.query"
        class="input-with-select"
        placeholder="请输入搜索内容"
      >
        <template #append>
          <el-button
            @click="
              queryModel.pagenum = 1;
              getList();
            "
            ><el-icon><Search /></el-icon
          ></el-button>
        </template>
      </el-input>
      <el-button
        type="primary"
        @click="
          router.push({ name: 'add' });
          model = {};
        "
        >添加商品</el-button
      >
    </div>
    <!-- 表格 -->
    <!-- <Table ></Table> -->

    <Table :lists="list" :column="col">
      <template v-slot:time="{ row: { add_time } }">
        {{ new Date(add_time * 1000).format() }}
      </template>
      <template v-slot:action="{ row }">
        <el-button
          size="small"
          type="primary"
          @click="
            model = { ...row };
            dialogFormVisible = true;
          "
          >编辑</el-button
        >
        <el-button size="small" type="danger" @click="getdelGoods(row.goods_id)"
          >删除</el-button
        >
      </template>
    </Table>

    <!-- 弹出框
    <el-dialog
      v-model="dialogFormVisible"
      :title="title"
      :close-on-click-modal="false"
    >
      <el-form
        ref="addref"
        :model="model"
        :rules="rules"
        label-width="120px"
        status-icon
      >
        <el-form-item label="商品名称" prop="goods_name">
          <el-input v-model="model.goods_name" />
        </el-form-item>
        <el-form-item label="商品价格" prop="goods_price">
          <el-input v-model="model.goods_price" />
        </el-form-item>
        <el-form-item label="商品重量" prop="goods_weight">
          <el-input v-model="model.goods_weight" />
        </el-form-item>
        <el-form-item label="商品数量" prop="goods_number">
          <el-input v-model="model.goods_number" maxlength="11" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="getAddGoods">确定</el-button>
        </span>
      </template>
    </el-dialog> -->

    <!-- 分页 -->
    <el-pagination
      @size-change="
        (v) => {
          queryModel.pagesize = v;
          getList();
        }
      "
      @current-change="
        (v) => {
          queryModel.pagenum = v;
          getList();
        }
      "
      :current-page="queryModel.pagenum"
      :page-sizes="[5, 10, 15, 20, 25, 30, 35, 40]"
      :page-size="queryModel.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
      <!-- :page-sizes="[1, 2, 3, 4]"-->
    </el-pagination>
  </div>
</template>

<script>
import { reactive, toRefs, ref, onMounted, computed } from "vue";
import api from "@/http/goods.js";
import { ElMessageBox, ElMessage } from "element-plus";
import { useRoute, useRouter } from "vue-router"; /** 引入router*/

export default {
  name: "users",
  setup() {
    /**
     * 路由
     */
    let router = useRouter();
    /**
     * 定义数据
     */
    const state = reactive({
      /**
       * 表单验证规则
       */
      rules: {
        goods_name: [
          {
            required: true,
            message: "请输入商品名称",
            trigger: "blur",
          },
          {
            min: 2,
            max: 50,
            message: "商品名称的长度在2～50个字",
            trigger: "blur",
          },
        ],
        goods_price: [
          {
            required: true,
            message: "请输入商品价格",
            trigger: "blur",
          },
        ],
        goods_weight: [
          {
            required: true,
            message: "请输入商品重量",
            trigger: "blur",
          },
        ],
        goods_number: [
          {
            required: true,
            message: "请输入商品数量",
            trigger: "blur",
          },
        ],
      },
      /**
       * 添加模型
       */
      model: {},
      /**
       * 控制弹出框显示隐藏
       */
      dialogFormVisible: false,
      /**
       * 搜素数据
       */
      queryModel: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      /**
       * 总条数
       */
      total: 0,
      /**
       * 用户模型
       */
      list: [],
      /**
       * 表格配置项
       */
      col: [
        {
          type: "index",
          label: "#",
        },
        {
          prop: "goods_name",
          label: "商品名称",
          width: "500",
        },
        {
          prop: "goods_price",
          label: "商品价格",
          width: "120",
          align: "center",
        },
        {
          label: "商品重量",
          prop: "goods_weight",
          width: "120",
          align: "center",
        },
        {
          label: "创建时间",
          slot: "time",
        },
        {
          label: "操作",
          slot: "action",
        },
      ],
    });

    /**
     * 删除用户
     */
    let getdelGoods = (id) => {
      ElMessageBox.confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          console.log(id);
          const res = await api.delGoods(id);
          // console.log(res, "row");
          if (res.meta.status == 200) {
            getList();
          }
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "已取消删除",
          });
        });
    };
    /**
     * 添加用户
     */
    let getAddGoods = async () => {
      const res = await (state.model.goods_id ? api.UpdataGoods : api.AddGoods)(
        state.model
      );

      if (res.meta.status == 200 || res.meta.status == 201) {
        state.dialogFormVisible = false;
        getList();
      }
    };
    /**
     * 获取数据
     */
    let getList = async () => {
      const res = await api.GoodsList(state.queryModel);
      console.log(res, "用户");
      if (res.meta.status == 200) {
        state.list = res.data.goods;
        state.total = res.data.total;
      }
      console.log(state.list, 111);
    };

    onMounted(() => {
      getList();
    });
    // /**
    //  * 标题
    //  */
    // let title = computed(() => {
    //   return state.model.goods_id ? "编辑用户 " : "添加用户";
    // });

    return {
      ...toRefs(state),
      getList, //渲染
      getAddGoods, //添加 编辑 用户
      getdelGoods, //删除用户
      router,
    };
  },
};
</script>

<style lang="scss" scoped>
* {
  box-sizing: border-box;
}

.users-wrapper {
  height: 100%;
  // 搜索盒子
  .search-wrapper {
    height: 50px;
    display: flex;
    align-items: center;
    .input-with-select {
      width: 400px;
      height: 40px;
      margin-right: 20px;
    }
  }
  // 表格
  .el-table {
    margin-top: 10px;
    margin-bottom: 15px;
  }
}
</style>